import Vue from 'vue'
import Router from 'vue-router'
import component from './component'
import http from '../utils/http'
import api from '../utils/api'

Vue.use(Router)
var routers = new Router({
    // mode: 'history',
    isAddDynamicMenuRoutes: false,
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: resolve => require(['../pages/login/login.vue'], resolve),
        },
        {
            path: '/home',
            component: resolve => require(['../pages/home/home.vue'], resolve),
            children: [


            ]
        },
    ]
})
routers.beforeEach((to, from, next) => {
    if (window.$cookies.get('token_back')) {//如果没有token信息则调到登录页
        if (routers.options.isAddDynamicMenuRoutes || isglobal(to)) {
            next()
        } else {
            creatRouter(to, from, next)
        }
    } else {
        if (to.path === '/login') {
            next()
            return false
        } else {
            next('/login')
            return false
        }
    }
})

function getComponent(url) {
    if (url.match(/(.*)(?=\/)/)) {
        console.log(url.match(/(.*)(?=\/)/)[0])
        return url.match(/(.*)(?=\/)/)[0]

    }
    return url
}

function isglobal(to, from, next) {
    let status = false
    const globalList = ['/login']
    if (globalList.indexOf(to.path) == 0) {
        status = true
    }
    return status
}

async function creatRouter(to, from, next) {
    let res = {
        menuList: [
            {
                appId: 4,
                icon: 'icon-coupons_fill',
                menuId: 1,
                name: '首页',
                open: null,
                orderNum: 0,
                parentId: 0,
                parentName: null,
                perms: null,
                type: 0,
                url: '/home',
                list: [
                    {
                        appId: 4,
                        icon: null,
                        list: Array(0),
                        menuId: 61,
                        name: '首页',
                        open: null,
                        orderNum: 0,
                        parentId: 60,
                        parentName: null,
                        perms: 'agriSubsidy:create',
                        type: 1,
                        url: 'homePage',
                    },
                ]
            },

            {
                appId: 4,
                icon: 'icon-coupons_fill',
                menuId: 1,
                name: '测试区域',
                open: null,
                orderNum: 0,
                parentId: 0,
                parentName: null,
                perms: null,
                type: 0,
                url: '/home',
                list: [
                    {
                        appId: 4,
                        icon: null,
                        list: Array(0),
                        menuId: 61,
                        name: '测试列表',
                        open: null,
                        orderNum: 0,
                        parentId: 60,
                        parentName: null,
                        perms: 'agriSubsidy:create',
                        type: 1,
                        url: 'dynamicList/page',
                    },
                    {
                        appId: 4,
                        icon: null,
                        list: Array(0),
                        menuId: 61,
                        name: '查看采购商详情',
                        open: null,
                        orderNum: 0,
                        parentId: 60,
                        parentName: null,
                        perms: 'agriSubsidy:create',
                        type: 3,
                        url: 'buyersDetail',
                    },
                    {
                        appId: 4,
                        icon: null,
                        list: Array(0),
                        menuId: 61,
                        name: '表单配置',
                        open: null,
                        orderNum: 0,
                        parentId: 60,
                        parentName: null,
                        perms: 'agriSubsidy:create',
                        type: 1,
                        url: 'dynamicFormCreate',
                    },
                    {
                        appId: 4,
                        icon: null,
                        list: Array(0),
                        menuId: 61,
                        name: '表单组件',
                        open: null,
                        orderNum: 0,
                        parentId: 60,
                        parentName: null,
                        perms: 'agriSubsidy:create',
                        type: 1,
                        url: 'dynamicFormDoc',
                    },
                    {
                        appId: 4,
                        icon: null,
                        list: Array(0),
                        menuId: 61,
                        name: '测试表单0',
                        open: null,
                        orderNum: 0,
                        parentId: 60,
                        parentName: null,
                        perms: 'agriSubsidy:create',
                        type: 1,
                        url: 'dynamicForm/12123',
                    },
                    {
                        appId: 4,
                        icon: null,
                        list: Array(0),
                        menuId: 61,
                        name: '表单-page',
                        open: null,
                        orderNum: 0,
                        parentId: 60,
                        parentName: null,
                        perms: 'agriSubsidy:create',
                        type: 1,
                        url: 'dynamicForm/page',
                    },
                ]
            },
        ],
        permissions: ['0', 'landTrans:list'],
        code: 200
    }
    if (res.code == 200) {
        sessionStorage.setItem('aside', JSON.stringify(res.menuList))
        sessionStorage.setItem('permissions', JSON.stringify(res.permissions))
        res.menuList.forEach((item, index) => {
            //menuList
            let parent = ''
            //父亲  查找父亲去匹配
            if (item.list && item.type !== 2) {
                if (item.url) {
                    routers.options.routes.forEach((val, index) => {
                        if (val.path == item.url) {
                            parent = val
                            //取到父亲
                        }
                    })
                }
                item.list.forEach((items) => {
                    let obj = {}
                    //循环数据 构造路由格式
                    console.log(getComponent(items.url))
                    obj = {
                        path: items.url,
                        name: items.url,
                        component: component[getComponent(items.url)],
                        children: [],
                        meta: {title: items.name}
                    }

                    if (items.list !== null && items.list.length > 0 && item.type !== 2) {

                        //三层加重定向  /${items.list[0].url}
                        let redirectUrl = ''
                        if (items.list[0].url != null) {
                            redirectUrl = `/${items.list[0].url}`
                            obj.redirect = `${parent.path}/${items.url}${redirectUrl}`
                        }

                        /*obj.redirect = `${parent.path}/${items.url}/list${ss++}`*/
                        items.list.forEach((it) => {

                            if (it.url !== null) {
                                obj.children.push(
                                    {
                                        path: it.url,
                                        name: it.url,
                                        meta: {title: it.name},
                                        component: component[it.url],
                                        children: []
                                    }
                                )

                            }

                        })
                    }
                    //push  到 父亲的 孩子下
                    if (parent.children) {
                        parent.children.push(obj)
                    }

                })

            }
        })
        //改变 判断 状态
        routers.options.isAddDynamicMenuRoutes = true
        //改变路由  实例化
        routers.addRoutes(routers.options.routes)
    }
    if (next) {
        next({...to, replace: true})
    }
}

export default routers
